import yaml
import os
from loguru import logger
from utils.object_utils import cache


# 返回项目路径，或者返回某个目录的路径
def get_path(dir,file_name=None):
    project_path = os.path.dirname(os.path.dirname(__file__))
    path = os.path.join(project_path,dir)
    print("path:", path)
    if file_name is not None:
        path = os.path.join(path,file_name)
    return path


# 读取yaml文件 ，返回Python对象
def read_yaml_file(yml_file):
    yml_path = get_path('data',yml_file)
    with open(yml_path,encoding='utf-8') as yml_obj:
        data = yaml.safe_load(yml_obj)
        logger.info("返回的测试用例数据为:{}".format(data))
        return data


# 判断传入的值是否包含了{{ ,若包含了{{ ，代表要从缓存中提取数据 ，否则直接返回原值 。
def is_contanins_brace(value):
    """
    {{user_id}}  , test02
    :param value:
    :return:
    """
    start_index = value.find("{{")
    end_index = value.find("}}")
    if start_index >= 0 and end_index >=0 :
        vls = cache.get(value[start_index+2:end_index])
        logger.debug("从缓存中提取的键名:{},值:{}".format(value[start_index+2:end_index],vls))
        return vls

    return value


if __name__ == '__main__':
    print(get_path('data','user_add_flow.yml'))
    print(read_yaml_file('user_add_flow.yml'))
    cache.set('user_id',100)
    print(is_contanins_brace("{{user_id}}"))